package com.ml.controller;

import com.ml.model.AjaxMsg;
import com.ml.model.MLUser;
import com.ml.service.MLUserService;
import com.ml.service.SystemService;
import com.ml.util.UtilMd5;
import com.ml.util.UtilProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

@Controller
public class IndexController {
    @Autowired
    private SystemService systemService;

    @Autowired
    private MLUserService mLUserService;






    @RequestMapping({"/index"})
    public String list() {
        return "page/category/category_index.jsp";
    }

    @RequestMapping(value = "/grid")
    public void grid(HttpServletRequest request, HttpServletResponse response) {

    }

    @RequestMapping({"/login"})
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("user");
        String passWord = request.getParameter("psd");
        if(!StringUtils.hasText(userName)){
            request.setAttribute("msg","用户名不能为空");
            return "index.jsp";
        }
        if(!StringUtils.hasText(passWord)){
            request.setAttribute("msg","密码不能为空");
            return "index.jsp";
        }
        String md5psd = UtilMd5.MD5(passWord);
        MLUser user = mLUserService.getByUserNameAndPsd(userName,md5psd);
        String adminstr = UtilProperties.getConfig("ml.admin.name");
        boolean isAdmin = false;
        if(user!=null){
            if(adminstr.contains(userName)  ){// //其他非sysadmin管理员账号，可以多个
                isAdmin = true;
            }
        }else{
            String passwordAdmin = UtilProperties.getConfig("ml.admin.password");
            if(adminstr.contains(userName) && passwordAdmin.equals(passWord) ){// sysadmin 管理员账号，只有一个
                user = new MLUser();
                user.setLoginid(userName);
                user.setId(1);
                user.setLastname("系统管理员");
                isAdmin = true;
             }else {//用户名或者密码错误
                request.setAttribute("msg","用户名或密码错误");
                return "index.jsp";
            }
        }
        request.getSession().setAttribute("mluser",user);
        if(isAdmin){
            request.getSession().setAttribute("flag","admin");
            //获取当年
            Calendar ca = Calendar.getInstance();
            Integer year = ca.get(Calendar.YEAR);
            request.setAttribute("year",year);
            return "redirect:tj/wk";
        }else{//其他用户暂时不开放页面
            Integer loctionid = user.getLocationid();
            if(loctionid==3){//天津用户
                return "redirect:tj/wk";
            }else{
                request.setAttribute("msg","暂无权限");
                return "index.jsp";
            }
        }
    }

    /**退出登陆*/
    @RequestMapping({"/lg"})
    public String lg(HttpServletRequest request) {
        //退出
        request.getSession().setAttribute("user",null);
        request.getSession().removeAttribute("user");
        request.getSession().setAttribute("flag",null);
        request.getSession().removeAttribute("flag");
        request.getSession().setAttribute("users",null);
        request.getSession().removeAttribute("users");

        return "index.jsp";
    }



    @RequestMapping({"/kx"})
    public String kx() {
        return "page/wk/wk_kx.jsp";
    }




    /**
     * 返回一个json对象
     */
    @RequestMapping({"/tb/card"})
    @ResponseBody
    public AjaxMsg tbCard(HttpServletRequest request, HttpServletResponse response) {
        return AjaxMsg.returnAjaxMsg(200,"同步成功!");
    }




    /**
     * 返回一个json对象
     */
    @RequestMapping({"/test"})
    @ResponseBody
    public AjaxMsg getById(HttpServletRequest request) {

        return AjaxMsg.returnAjaxMsg();
    }

}
